package com.ff.xyh.service.message.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.message.RoomBanned;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.service.message.service.RoomBannedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群聊禁入表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-11-28
 */
@RestController
@RequestMapping("/message-service")
public class RoomBannedController {
    @Autowired
    private RoomBannedService bannedService;

    @PostMapping("get-room-banned-room-banner")
    public ResultObject getRoomBannedByRoomIdAndBanner(
            @RequestParam(name = "roomId") String roomId,
            @RequestParam(name = "bannerId") String bannerId) {
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId)
                .eq("banner_id", bannerId);
        RoomBanned roomBanned = bannedService.getOne(wrapper);
        if (roomBanned == null) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("data", roomBanned);
        }
    }

    @PostMapping("get-room-banned-room")
    public ResultObject getRoomBannedInRoom(
            @RequestParam(name = "roomId") String roomId) {
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId);
        List<RoomBanned> list = bannedService.list(wrapper);
        if (list == null) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", list);
        }
    }
    @PostMapping("get-room-banned-operator")
    public ResultObject getRoomBannedWithOperator(
            @RequestParam(name = "operatorId") String operatorId) {
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("operator_id", operatorId);
        List<RoomBanned> list = bannedService.list(wrapper);
        if (list == null) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", list);
        }
    }
    @PostMapping("get-room-banned-banner")
    public ResultObject getRoomBannedWithBanner(
            @RequestParam(name = "bannerId") String bannerId) {
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("banner_id", bannerId);
        List<RoomBanned> list = bannedService.list(wrapper);
        if (list == null) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", list);
        }
    }

    @PostMapping("add-room-banned")
    public ResultObject addRoomBanned(
            @RequestBody RoomBanned roomBanned) {
        if (roomBanned.getRoomId() == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
        if (roomBanned.getBannerId() == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
//        if (roomBanned.getOperatorId() == null) {
//            return ResultObject.error().message(ResultMessage.ERROR_ARG);
//        }
        if (bannedService.save(roomBanned)) {
            return ResultObject.ok().data("data", roomBanned);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }

//    @PostMapping("update-room-banned")
//    public ResultObject updateRoomBanned(
//            @RequestBody RoomBanned roomBanned) {
//        String roomId = roomBanned.getRoomId();
//        String operatorId = roomBanned.getOperatorId();
//        String bannerId = roomBanned.getBannerId();
//        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
//        wrapper.eq("room_id", roomId)
//                .eq("operator_id", operatorId)
//                .eq("banner_id", bannerId);
//        if (bannedService.update(roomBanned, wrapper)) {
//            return ResultObject.ok().data("data", roomBanned);
//        } else {
//            return ResultObject.error().message(ResultMessage.NO_RESULT);
//        }
//    }

    @PostMapping("delete-room-banned")
    public ResultObject deleteRoomBanned(
            @RequestBody RoomBanned roomBanned) {
        String roomId = roomBanned.getRoomId();
        String operatorId = roomBanned.getOperatorId();
        String bannerId = roomBanned.getBannerId();
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId)
                .eq("operator_id", operatorId)
                .eq("banner_id", bannerId);
        roomBanned = bannedService.getOne(wrapper);
        if (roomBanned != null) {
            if (bannedService.remove(wrapper)) {
                return ResultObject.ok().data("data", roomBanned);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        } else {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
    }

    @PostMapping("delete-room-banned-room-banner")
    public ResultObject deleteRoomBannedByRoomIdAndBannerId(
            @RequestParam(name = "roomId") String roomId,
            @RequestParam(name = "bannerId") String bannerId) {
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId)
                .eq("banner_id", bannerId);
        RoomBanned roomBanned = bannedService.getOne(wrapper);
        if (roomBanned != null) {
            if (bannedService.remove(wrapper)) {
                return ResultObject.ok().data("data", roomBanned);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        } else {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        }
    }

    @PostMapping("delete-room-banned-room")
    public ResultObject deleteRoomBannedInRoom(
            @RequestParam(name = "roomId") String roomId) {
        QueryWrapper<RoomBanned> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId);
        List<RoomBanned> list = bannedService.list(wrapper);
        if (list == null || list.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (bannedService.remove(wrapper)) {
                return ResultObject.ok().data("items", list);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }


}

